package com.chuanyang.driverh.mainstart;


import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RatingBar;
import android.widget.ScrollView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainhome.BidOrderActivity;
import com.chuanyang.driverh.mainorder.GoodOwnerInfoActivity;
import com.chuanyang.driverh.mainorder.adapter.OrderInfoAdapter;
import com.chuanyang.driverh.mainorder.adapter.OrderPassPointAdapter;
import com.chuanyang.driverh.mainstart.adapter.VarPlaceAdapter;
import com.chuanyang.driverh.responseresult.DriverInfoData;
import com.chuanyang.driverh.responseresult.InvoceOrderData;
import com.chuanyang.driverh.responseresult.OrderDatailResult;
import com.chuanyang.driverh.responseresult.OrderDetailData;
import com.chuanyang.driverh.utils.BitmapUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.LogUtils;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.TimeUtils;
import com.chuanyang.driverh.utils.eventbus.SubscribeEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;
import com.chuanyang.driverh.utils.widget.CustomMeasureHeightListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

public class NewInvoceOrderInfoActivity extends BaseTitleActivity {
    private ImageView more;
    private CustomMeasureHeightListView lv_varpalaces;
    private VarPlaceAdapter adapter;
    private RatingBar ratingBar;

    private ImageView iv_peopleicon;
    private ArrayList<OrderDetailData.PassPointsBean> passpointlist;
    private ArrayList<OrderDetailData.PassPointsBean> newpasspointlist;
    public ArrayList<OrderDetailData.PassPointsBean> goodInfolist;
    public int index = 0;
    private TextView tv_peoplename;
    private TextView tv_phone;
    private InvoceOrderData.DataBean order;
    private TextView tv_orderdate;

    private TextView tv_compete;
    //private EditText tv_bid;
    private HashMap params;
    private TextView tv_oredrinfo_deliverydate;
    private TextView tv_oredrinfo_picuupdate;
    private CustomMeasureHeightListView lv_orderlist;
    private DriverInfoData driverinfo;
    private ScrollView sc_invoceorderinfo;
    private ArrayList fillpointlist;
    private OrderDetailData.PassPointsBean startPoint;
    private OrderDetailData.PassPointsBean endPoint;
    private OrderPassPointAdapter passPointAdapter;
    private TextView tv_oredrinfo_dingyue;
    private LinearLayout ll_oredrinfo_goodownerdiv;
    private OrderDetailData orderDetailData;
    private TextView tv_oredrinfo_remark;

    private int totalprice;
    private double unitprice;
    private TextView tv_oredrinfo_orderid;
    private TextView tv_oredrinfodetail_taskname;
    private TextView tv_oredrinfo_tradetype;
    private LinearLayout ll_orderinfo_cartimediv;
    private LinearLayout ll_orderinfo_shiptimediv;
    private TextView tv_driverinfo_loaddatebeg;
    private TextView tv_driverinfo_loaddateend;
    private TextView tv_driverinfo_stevedoreDaybeg;
    private TextView tv_driverinfo_stevedoreDayend;
    private TextView tv_oredrinfodetail_demurrage;

    private boolean isTotalPrice = false;//默认竞价的是单价
    private LinearLayout ll_oredrinfo_lowestprice;
    private TextView tv_oredrinfo_lowestprice;

private String orderId="";

    @Override
    protected int getContentResId() {
        return R.layout.common_act_newinvoceorderinfo;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        //单价
        sc_invoceorderinfo = (ScrollView) findViewById(R.id.sc_invoceorderinfo);
        iv_peopleicon = (ImageView) findViewById(R.id.iv_oredrinfo_peopleicon);
        ratingBar = (RatingBar) findViewById(R.id.rb_orderinfo_stars);
        tv_peoplename = (TextView) findViewById(R.id.tv_oredrinfo_peoplename);
        //车辆订单时显示的界面
        ll_orderinfo_cartimediv = (LinearLayout) findViewById(R.id.ll_orderinfo_cartimediv);
        tv_oredrinfo_deliverydate = (TextView) findViewById(R.id.tv_oredrinfo_deliverydate);//发货时间
        tv_oredrinfo_picuupdate = (TextView) findViewById(R.id.tv_oredrinfo_picuupdate);//取货时间
        //船只订单时显示的界面
        ll_orderinfo_shiptimediv = (LinearLayout) findViewById(R.id.ll_orderinfo_shiptimediv);
        tv_driverinfo_loaddatebeg = (TextView) findViewById(R.id.tv_driverinfo_loaddatebeg);
        tv_driverinfo_loaddateend = (TextView) findViewById(R.id.tv_driverinfo_loaddateend);
        tv_driverinfo_stevedoreDaybeg = (TextView) findViewById(R.id.tv_driverinfo_stevedoreDaybeg);
        tv_driverinfo_stevedoreDayend = (TextView) findViewById(R.id.tv_driverinfo_stevedoreDayend);
        //滞期费
        tv_oredrinfodetail_demurrage = (TextView) findViewById(R.id.tv_oredrinfodetail_demurrage);
        //-----------------------
        tv_phone = (TextView) findViewById(R.id.tv_oredrinfo_phone);
        ll_oredrinfo_goodownerdiv = (LinearLayout) findViewById(R.id.ll_oredrinfo_goodownerdiv);
        tv_oredrinfo_dingyue = (TextView) findViewById(R.id.tv_oredrinfo_dingyue);//订阅
        tv_orderdate = (TextView) findViewById(R.id.tv_oredrinfo_orderdate);//发单时间
        tv_oredrinfo_remark = (TextView) findViewById(R.id.tv_oredrinfo_remark);

        tv_oredrinfo_deliverydate = (TextView) findViewById(R.id.tv_oredrinfo_deliverydate);//发货时间
        tv_oredrinfo_picuupdate = (TextView) findViewById(R.id.tv_oredrinfo_picuupdate);//取货时间
        //订单号
        tv_oredrinfo_orderid = (TextView) findViewById(R.id.tv_oredrinfo_orderid);
        tv_compete = (TextView) findViewById(R.id.tv_oredrinfo_compete);
        //外贸时才显示
        tv_oredrinfo_tradetype = (TextView) findViewById(R.id.tv_oredrinfo_tradetype);//贸易类型
        tv_oredrinfodetail_taskname = (TextView) findViewById(R.id.tv_oredrinfodetail_taskname);//任务名称

        //最低报价
        ll_oredrinfo_lowestprice = (LinearLayout) findViewById(R.id.ll_oredrinfo_lowestprice);
        tv_oredrinfo_lowestprice = (TextView) findViewById(R.id.tv_oredrinfo_lowestprice);

        lv_varpalaces = (CustomMeasureHeightListView) findViewById(R.id.ll_oredrinfo_varpalace);
        lv_orderlist = (CustomMeasureHeightListView) findViewById(R.id.ll_orderinfo_orderlist);
    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("订单信息 (竞价)");
        showActivityLoading();
        orderId=getIntent().getStringExtra("orderId");
        order = (InvoceOrderData.DataBean)  getIntent().getSerializableExtra("order");
        requsetData();
    }

    @Override
    protected void initListener() {
        ll_oredrinfo_goodownerdiv.setOnClickListener(this);
        tv_compete.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.tv_oredrinfo_compete:
                startActivity(BidOrderActivity.class,orderDetailData);
                break;
            case R.id.ll_oredrinfo_goodownerdiv:
                if (orderDetailData != null) {
                    startActivity(GoodOwnerInfoActivity.class, orderDetailData);
                }
                break;

        }
    }

    public void onEventMainThread(SubscribeEvent event) {
        if (event.isChangeSubscribed()) {
            requsetData();
        }

    }



    private void fillView(OrderDetailData data) {
        BitmapUtil.showImgForNetPath( order.getUserImage(),iv_peopleicon);
        //   ratingBar.setRating(order.getS);后期添加评价
        tv_phone.setText(order.getPhone());
        tv_orderdate.setText(TimeUtils.getStandardTime(data.getOrder_time()));
        tv_oredrinfo_orderid.setText(data.getOrderNo());
        if(!TextUtils.isEmpty(data.getAssignment()))
        {
            tv_oredrinfodetail_taskname.setText(data.getAssignment());
        }
        else {
            tv_oredrinfodetail_taskname.setText("");
        }
        if(data.getCurrency()==2)//订单外贸类型（1人民币，2美元）
        {
            tv_oredrinfo_tradetype.setVisibility(View.VISIBLE);
            if(TextUtils.isEmpty(order.getMinPrice()))
            {
                ll_oredrinfo_lowestprice.setVisibility(View.GONE);
            }
            else {
                tv_oredrinfo_lowestprice.setText(order.getMinPrice()+"$");
            }

        }
        else {
            tv_oredrinfo_tradetype.setVisibility(View.GONE);
            if(TextUtils.isEmpty(order.getMinPrice()))
            {
                ll_oredrinfo_lowestprice.setVisibility(View.GONE);
            }
            else {
                tv_oredrinfo_lowestprice.setText(order.getMinPrice()+"￥");
            }
        }
        if (data.getTransPortWay() == 1)//车
        {
            tv_oredrinfo_deliverydate.setText(TimeUtils.getStandardTime(data.getOrder_startime()));
            tv_oredrinfo_picuupdate.setText(TimeUtils.getStandardTime(data.getOrder_endtime()));
            ll_orderinfo_cartimediv.setVisibility(View.VISIBLE);
            ll_orderinfo_shiptimediv.setVisibility(View.GONE);

        } else if (data.getTransPortWay() == 2)//船
        {
            tv_driverinfo_loaddatebeg.setText(data.getLoadDate());
            tv_driverinfo_loaddateend.setText(TimeUtils.getStandardTime(data.getOrder_endtime()));
            tv_driverinfo_stevedoreDaybeg.setText(data.getStevedoreDay());
            tv_driverinfo_stevedoreDayend.setText(data.getFq());
            tv_oredrinfodetail_demurrage.setText(data.getDemurrage()+data.getUnit()+"/天/吨");
            ll_orderinfo_cartimediv.setVisibility(View.GONE);
            ll_orderinfo_shiptimediv.setVisibility(View.VISIBLE);

        }
        if (data.getSubscriptionStatus() == 1) {
            tv_oredrinfo_dingyue.setText("已订阅");

        } else if (data.getSubscriptionStatus() == 2) {
            tv_oredrinfo_dingyue.setText("未订阅");

        }
        if (!TextUtils.isEmpty(order.getUsername())) {
            tv_peoplename.setText(order.getUsername());
        } else {
            tv_peoplename.setText("---");

        }
        if (!TextUtils.isEmpty(data.getRemark())) {
            tv_oredrinfo_remark.setText(data.getRemark());
        } else {
            tv_oredrinfo_remark.setText("");

        }
        passpointlist = new ArrayList();
        //需要传入到适配器的数据
        fillpointlist = new ArrayList();
        for (OrderDetailData.PassPointsBean passPointsBean : data.getPassPoints()) {
            if (passPointsBean.getPointType().equals("s")) {
                startPoint = passPointsBean;
            } else if (passPointsBean.getPointType().equals("p")) {
                passpointlist.add(passPointsBean);
            } else {
                endPoint = passPointsBean;

            }
        }
        OrderInfoAdapter orderInfoAdapter = new OrderInfoAdapter(this, data.getDetailList());
        lv_orderlist.setAdapter(orderInfoAdapter);
        fillpointlist.clear();
        fillpointlist.add(startPoint);
        lv_varpalaces.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (passpointlist.size() > 0) {
                    if (0 < position && position < passpointlist.size() + 1) {
                        fillpointlist.clear();
                        fillpointlist.add(startPoint);
                        if ((index++) % 2 == 1) {
                            fillpointlist.addAll(passpointlist);
                            fillpointlist.add(endPoint);

                        } else {
                            fillpointlist.add(passpointlist.get(0));
                            fillpointlist.add(endPoint);

                        }

                        passPointAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
        if (passpointlist.size() > 0) {
            fillpointlist.add(passpointlist.get(0));
            LogUtils.e("OrderPassPointAdapter", fillpointlist.size() + "---------11");


        }
        fillpointlist.add(endPoint);
        passPointAdapter = new OrderPassPointAdapter(this, fillpointlist);
        //设置数据适配器
        lv_varpalaces.setAdapter(passPointAdapter);
        //滚动到原点  注意这两个方法需要一起使用 前者打断动画，后者实现移动
        sc_invoceorderinfo.scrollTo(0, 0);
        sc_invoceorderinfo.smoothScrollTo(0, 0);
        hideActivityLoading();
    }


    private void requsetData() {
        DialogUtil.showLoadingDialog(activityContext,"加载中...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("driverId", SharedPrefUtil.getUserId());

        if (!StringUtil.isBlank(orderId)){
            params.put("orderId", orderId);
        }else{
            params.put("orderId", order.getOrderId() + "");
        }

        MyHttpUtil.selectOrderMsgById(activityContext,params, new MyCallback<OrderDatailResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }
            @Override
            public void onSuccess(OrderDatailResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    orderDetailData = result.getData();
                    fillView(orderDetailData);
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });

    }


}
